package cn.com.headfree.aoutdata.service;

import cn.com.headfree.aoutdata.dao.OutDataDao;
import cn.com.headfree.aoutdata.model.XuanChengDeptAndUser;
import cn.com.headfree.depterprela.service.OrgDeptService;
import cn.com.headfree.entity.*;
import cn.com.headfree.userinfo.dao.IUserInfoDao;
import cn.com.headfree.util.DateUtils;
import cn.com.headfree.vo.SysDictionaryInfoVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Description: TODO
 * @Author wcSun
 * @Date 2021/9/27
 **/
@Service("outDataService")
public class OutDataService{

    @Autowired
    private OrgDeptService orgDeptService;

    @Autowired
    private OutDataDao outDataDao;

    @Autowired
    private IUserInfoDao userInfoDao;

    private int sort;

    public TOrgDept getOrgDept(String orgId){
        TOrgDept dept = orgDeptService.getOrgDeptById(orgId);
        return dept;
    }

    void org(){

    }

    public void impoDept(String orgId){
        List<TOrgDept> list = outDataDao.findmaxSort();
        sort = list.get(0).getSort();
        TOrgDept pdept = orgDeptService.getOrgDeptById(orgId);
        String ppath = pdept.getPath();
        List<TOrgDept> orgList = outDataDao.findAll();
        if(CollectionUtils.isNotEmpty(orgList)){
            for (TOrgDept item :orgList){
                TOrgDept orgdept = insetOrgDept(item,orgId,ppath);
                List<TOrgDept> dept = outDataDao.selectOrgfromXUN(orgdept.getId());
                for (TOrgDept itemp :dept) {
                    insetOrgDept(itemp,orgdept.getId(),orgdept.getPath());
                }
            }
        }
    }


    public TOrgDept insetOrgDept(TOrgDept item,String orgId,String ppath){
        TOrgDept orgdept = orgDeptService.getOrgDeptById(item.getId());
        if(orgdept==null){
            orgdept = new TOrgDept();
            orgdept.setId(item.getId().trim());
            orgdept.setName(item.getName());
            orgdept.setpId(orgId);
            orgdept.setPath(String.format("%s|%s",ppath,item.getId().trim()));
            String[] arr = orgdept.getPath().split("\\|");
            orgdept.setLevel(String.valueOf(arr.length));
            orgdept.setIsAuditUnit(1);
            orgdept.setIsHeadDept(1);
            orgdept.setSort(sort++);
            outDataDao.insertOrgDept(orgdept);
        }
        return orgdept;
    }



    public void importEmbase(){
        List<XuanChengDeptAndUser> allList = outDataDao.selectxuancheng();
        int i=0;
        for(XuanChengDeptAndUser item: allList){
            try {
                //人员基础信息
                saveEmbase(item);
                //部门人员关系
                saveDeptEmpRela(item);

                //学校学历学位
                saveEducate(item);

                //专业技术资格
                saveProtec(item);

                //专业技能等级
                saveSkillLevel(item);

                //专家人才
                saveExpertTitle(item);

                i++;
            }catch(Exception e){
                System.out.println(item.getName()+"导入失败！");
            }
        }
        System.out.println("***************  结束********************  导入个数统计：" + i);
    }


    /**
     *人员基础信息
     */
    void saveEmbase(XuanChengDeptAndUser item){
        List<TEmpBaseInfo> empList = outDataDao.findEmpBaseByErpNo(item.getErpNo());
        if (CollectionUtils.isEmpty(empList)) {
            String uuid = UUID.randomUUID().toString().replace("-", "");
            TEmpBaseInfo tmpBaseInfo = new TEmpBaseInfo();
            tmpBaseInfo.setId(uuid);
            tmpBaseInfo.setErpNo(item.getErpNo());
            tmpBaseInfo.setName(item.getName());
            String sex = "男".equals(item.getSex()) ? "1" : "2";
            tmpBaseInfo.setSex(sex);
            tmpBaseInfo.setBirthDate(DateUtils.parseDateStrToDate(item.getBirthDate()));
            String nation = null;
            SysDictionaryInfoVo vo1 = outDataDao.getSysDictionaryInfo("民族", item.getNation());
            if (vo1 != null) {
                nation = vo1.getValue();
            }
            tmpBaseInfo.setNation(nation);
            tmpBaseInfo.setBirthPlace(item.getJg());
            tmpBaseInfo.setBornPlace(item.getJg());
            String politiStatus = null;
            SysDictionaryInfoVo vo2 = outDataDao.getSysDictionaryInfo("政治面貌", item.getPolitiStatus());
            if (vo2 != null) {
                politiStatus = vo2.getValue();
            }
            tmpBaseInfo.setPolitiStatus(politiStatus);
            tmpBaseInfo.setIdCardNo(item.getIdCardNo());
//                  tmpBaseInfo.setMobilePhone();
//                  tmpBaseInfo.setLevel();
//                  tmpBaseInfo.setSalaryGrade();
//                  tmpBaseInfo.setPartyDate();
            tmpBaseInfo.setJobDate(DateUtils.parseDateStrToDate(item.getJobDate()));
            tmpBaseInfo.setUnit(item.getOrgName());
            tmpBaseInfo.setDept(item.getDeptName());
            String employeeLevel = null;
            SysDictionaryInfoVo vo3 = outDataDao.getSysDictionaryInfo("职员级别", item.getZyjb());
            if(vo3!=null){
                employeeLevel = vo3.getValue();
            }
            tmpBaseInfo.setEmployeeLevel(employeeLevel);

            String postLevel = null;
            SysDictionaryInfoVo vo4 = outDataDao.getSysDictionaryInfo("职务级别", item.getZhuwulevel());
            if(vo4!=null){
                postLevel = vo4.getValue();
            }
            tmpBaseInfo.setPostLevel(postLevel);
            tmpBaseInfo.setCurPost(item.getZhiwuname());
            tmpBaseInfo.setRealPost(item.getGwname());
//                  tmpBaseInfo.setFuturePost();
            tmpBaseInfo.setOrgId(item.getOrgDeptId());
            tmpBaseInfo.setCreaterId(item.getErpNo());
            tmpBaseInfo.setCreaterName(item.getName());
            tmpBaseInfo.setCreateTime(new Date());
            tmpBaseInfo.setUpdaterId(item.getErpNo());
            tmpBaseInfo.setUpdaterName(item.getName());
            tmpBaseInfo.setUpdateTime(new Date());
            tmpBaseInfo.setDelSign(0);
            tmpBaseInfo.setIsValid(0);
//            tmpBaseInfo.setAuditState();
            tmpBaseInfo.setOrignalId(uuid);
            tmpBaseInfo.setFlowStatus(1);
//            tmpBaseInfo.setBusinessTypeId();
//            tmpBaseInfo.setIscUserId();
            tmpBaseInfo.setPrimitiveId(uuid);
//            tmpBaseInfo.setSort();
            outDataDao.addBaseInfo(tmpBaseInfo);
        }
    }

    /**
     * 部门人员关系
     */
    void saveDeptEmpRela(XuanChengDeptAndUser item){
        List<TDeptEmpRela> deptEmpRelaList = outDataDao.findDeptEmpRelaByErpNo(item.getErpNo());
        if (CollectionUtils.isEmpty(deptEmpRelaList)) {
            String uuid = UUID.randomUUID().toString().replace("-", "");
            TDeptEmpRela deptEmpRela = new TDeptEmpRela();
            deptEmpRela.setId(uuid);
            deptEmpRela.setErpNo(item.getErpNo());
            deptEmpRela.setOrgId(item.getOrgDeptId());
            deptEmpRela.setDeptId(item.getDempNo());
            outDataDao.insertDeptEmpRela(deptEmpRela);
        }
    }

    void saveEducate(XuanChengDeptAndUser item){
        if(StringUtils.isBlank(item.getSchoole())){
            return;
        }
        List<TEducateInfo> educateList = userInfoDao.getEducateInfoByErpNo(item.getErpNo(),null);
        if (CollectionUtils.isEmpty(educateList)) {
            String uuid = UUID.randomUUID().toString().replace("-", "");
            TEducateInfo tEducateInfo = new TEducateInfo();
            tEducateInfo.setId(uuid);
//            tEducateInfo.setStartTime();
//            tEducateInfo.setEndTime();
            tEducateInfo.setAcquTime(DateUtils.parseDateStrToDate(item.getBydate()));
            tEducateInfo.setGradSchool(item.getSchoole());
//            tEducateInfo.setSchoolType();
            tEducateInfo.setMajor(item.getZhaungye());
            String xueli = null;
            SysDictionaryInfoVo vo1 = outDataDao.getSysDictionaryInfo("人员学历", item.getXueli());
            if(vo1!=null){
                xueli = vo1.getValue();
            }
            tEducateInfo.setEducName(xueli);
            String xuewei = null;
            SysDictionaryInfoVo vo2 = outDataDao.getSysDictionaryInfo("学位", item.getXuwei());
            if(vo2!=null){
                xuewei = vo2.getValue();
            }
            tEducateInfo.setDegreeName(xuewei);
            if(item.getXueli()!=null){
                String qrizzgxl = item.getQrizzgxl();//全日制最高学历
                String jyxueli = item.getJyxueli();//就业学历
                tEducateInfo.setIsContEduc(item.getXueli().equals(qrizzgxl)?"0":"1");//继续教育
                tEducateInfo.setIsEmployEduc(item.getXueli().equals(jyxueli)?"0":"1");//就业
                tEducateInfo.setMajor(item.getXueli().equals(jyxueli)?item.getJyzymc():item.getZhaungye());
            }
            tEducateInfo.setIsHighestEduc("0");
            tEducateInfo.setErpNo(item.getErpNo());
            tEducateInfo.setUserName(item.getName());
            tEducateInfo.setIdCardNo(item.getIdCardNo());
            tEducateInfo.setOrgId(item.getOrgDeptId());
            tEducateInfo.setCreaterId(item.getErpNo());
            tEducateInfo.setCreaterName(item.getName());
            tEducateInfo.setCreateTime(new Date());
            tEducateInfo.setUpdaterId(item.getErpNo());
            tEducateInfo.setUpdaterName(item.getName());
            tEducateInfo.setUpdateTime(new Date());
            tEducateInfo.setDelSign(0);
            tEducateInfo.setIsValid(0);
//            tEducateInfo.setAuditState();
            tEducateInfo.setOrignalId(uuid);
            tEducateInfo.setFlowStatus(1);
            tEducateInfo.setBusinessTypeId("eabf26f435d54e22b9f3a37912856508");
//            tEducateInfo.setIscUserId();
            tEducateInfo.setPrimitiveId(uuid);
            tEducateInfo.setIsCover(0);
            userInfoDao.addEducateInfo(tEducateInfo);
        }
    }

    /**
     * 专业技术资格信息
     * @param item
     */
    void saveProtec(XuanChengDeptAndUser item){
        if(StringUtils.isBlank(item.getZjname())){
            return;
        }
        List<TProTecInfo> tProTecInfoList = userInfoDao.getProTecInfoByErpNo(item.getErpNo());
        if(CollectionUtils.isEmpty(tProTecInfoList)){
            String uuid = UUID.randomUUID().toString().replace("-", "");
            TProTecInfo tProTecInfo = new TProTecInfo();
            tProTecInfo.setId(uuid);
            tProTecInfo.setErpNo(item.getErpNo());
            tProTecInfo.setUserName(item.getName());
            tProTecInfo.setIdCardNo(item.getIdCardNo());
            if(StringUtils.isNotBlank(item.getZjtime())){
                tProTecInfo.setAcquTime(DateUtils.parseDateStrToDate(item.getZjtime()));
                tProTecInfo.setPostTime(DateUtils.parseDateStrToDate(item.getZjtime()));
            }
            tProTecInfo.setSubjName(item.getZjname());
            String zjxili = null;
            SysDictionaryInfoVo vo1 = outDataDao.getSysDictionaryInfo("专业技术资格所属专业", item.getZjxili());
            if(vo1!=null){
                zjxili = vo1.getValue();
            }
            tProTecInfo.setSubjSet(zjxili);

            String zjlevel = null;
            SysDictionaryInfoVo vo2 = outDataDao.getSysDictionaryInfo("职称等级", item.getZjlevel());
            if(vo2!=null){
                zjlevel = vo2.getValue();
            }
            tProTecInfo.setSubjLevel(zjlevel);
//            tProTecInfo.setAuditOrg();
//            tProTecInfo.setOrgLevel();
//            tProTecInfo.setCertNo();
//            tProTecInfo.setOrgId();
            tProTecInfo.setCreaterId(item.getErpNo());
            tProTecInfo.setCreaterName(item.getName());
            tProTecInfo.setCreateTime(new Date());
            tProTecInfo.setUpdaterId(item.getErpNo());
            tProTecInfo.setUpdaterName(item.getName());
            tProTecInfo.setUpdateTime(new Date());
            tProTecInfo.setDelSign(0);
            tProTecInfo.setIsValid(0);
//            tProTecInfo.setAuditState();
            tProTecInfo.setOrignalId(uuid);
            tProTecInfo.setFlowStatus(1);
            tProTecInfo.setBusinessTypeId("eabf26f435d54e22b9f3a37912856510");
//            tProTecInfo.setIscUserId();
            tProTecInfo.setPrimitiveId(uuid);
            tProTecInfo.setIsCover(0);

            userInfoDao.addProTecInfo(tProTecInfo);
        }
    }

    /**
     * 专业技能等级
     * @param item
     */
    void saveSkillLevel(XuanChengDeptAndUser item){
        if(StringUtils.isBlank(item.getZylevel())){
            return;
        }
        List<TSkillLevelInfo> skillLevelList = userInfoDao.getSkillLevelInfoByErpNo(item.getErpNo());
        if(CollectionUtils.isEmpty(skillLevelList)){
            String uuid = UUID.randomUUID().toString().replace("-", "");
            TSkillLevelInfo sillLevelInfo = new TSkillLevelInfo();
            sillLevelInfo.setId(uuid);
            sillLevelInfo.setErpNo(item.getErpNo());
            sillLevelInfo.setUserName(item.getName());
            sillLevelInfo.setIdCardNo(item.getIdCardNo());
            if(StringUtils.isNotBlank(item.getZydate())){
                sillLevelInfo.setAcquTime(DateUtils.parseDateStrToDate(item.getZydate()));
                sillLevelInfo.setPostTime(DateUtils.parseDateStrToDate(item.getZydate()));
            }
            String jngz = null;
            SysDictionaryInfoVo vo1 = outDataDao.getSysDictionaryInfo("技能鉴定工种", item.getJngz());
            if(vo1!=null){
                jngz = vo1.getValue();
            }
            sillLevelInfo.setTechType(jngz);//技能鉴定工种
            String zylevel = null;
            SysDictionaryInfoVo vo2 = outDataDao.getSysDictionaryInfo("资格等级", item.getZylevel());
            if(vo2!=null){
                zylevel = vo2.getValue();
            }
            sillLevelInfo.setQualifiLevel(zylevel);//资格等级
//            sillLevelInfo.setAuditOrg();
//            sillLevelInfo.setOrgLevel();
//            sillLevelInfo.setCertNo();
            sillLevelInfo.setOrgId(item.getOrgDeptId());
            sillLevelInfo.setCreaterId(item.getErpNo());
            sillLevelInfo.setCreaterName(item.getName());
            sillLevelInfo.setCreateTime(new Date());
            sillLevelInfo.setUpdaterId(item.getErpNo());
            sillLevelInfo.setUpdaterName(item.getName());
            sillLevelInfo.setUpdateTime(new Date());
            sillLevelInfo.setDelSign(0);
            sillLevelInfo.setIsValid(0);
//            sillLevelInfo.setAuditState();
            sillLevelInfo.setOrignalId(uuid);
            sillLevelInfo.setFlowStatus(1);
            sillLevelInfo.setBusinessTypeId("eabf26f435d54e22b9f3a37912856511");
//            sillLevelInfo.setIscUserId();
            sillLevelInfo.setPrimitiveId(uuid);
            sillLevelInfo.setIsCover(0);
            userInfoDao.addSkillLevelInfo(sillLevelInfo);
        }


    }

    /**
     * 专家人才
     * @param item
     */
    void saveExpertTitle(XuanChengDeptAndUser item){
        if(StringUtils.isBlank(item.getZjrclx())){
            return;
        }
        List<TExpertTitleInfo> expertTitleList = userInfoDao.getExpertTitleInfoByErpNo(item.getErpNo());
        if(CollectionUtils.isEmpty(expertTitleList)){
            String uuid = UUID.randomUUID().toString().replace("-", "");
            TExpertTitleInfo expertTitleInfo = new TExpertTitleInfo();
            expertTitleInfo.setId(uuid);
            expertTitleInfo.setErpNo(item.getErpNo());
            expertTitleInfo.setUserName(item.getName());
            expertTitleInfo.setIdCardNo(item.getIdCardNo());
//            expertTitleInfo.setMajor();
//            expertTitleInfo.setPostTime();
//            expertTitleInfo.setAcquTime();
//            expertTitleInfo.setLevelName();
//            expertTitleInfo.setType();
//            expertTitleInfo.setEmpTermStart();
//            expertTitleInfo.setEmpTermEnd();
            String zjrclx = null;
            SysDictionaryInfoVo vo1 = outDataDao.getSysDictionaryInfo("人才称号", item.getZjrclx());
            if(vo1!=null){
                zjrclx = vo1.getValue();
            }
            expertTitleInfo.setEmpTitle(zjrclx);
            expertTitleInfo.setOrgId(item.getOrgDeptId());
            expertTitleInfo.setCreaterId(item.getErpNo());
            expertTitleInfo.setCreaterName(item.getName());
            expertTitleInfo.setCreateTime(new Date());
            expertTitleInfo.setUpdaterId(item.getErpNo());
            expertTitleInfo.setUpdaterName(item.getName());
            expertTitleInfo.setUpdateTime(new Date());
            expertTitleInfo.setDelSign(0);
            expertTitleInfo.setIsValid(0);
//            expertTitleInfo.setAuditState();
            expertTitleInfo.setOrignalId(uuid);
            expertTitleInfo.setFlowStatus(1);
            expertTitleInfo.setBusinessTypeId("eabf26f435d54e22b9f3a37912856512");
//            expertTitleInfo.setIscUserId();
            expertTitleInfo.setPrimitiveId(uuid);
            expertTitleInfo.setIsCover(0);
            userInfoDao.addExpertTitleInfo(expertTitleInfo);
        }
    }
}
